package GUI;

import Objetos.Producto;
import Objetos.Sesion;
import Http.ResponseCallback;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.system.GIFEncodedImage;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.FullScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
/**
 * 
 * @author ARMGARCES
 *
 */
public class WaitScreen extends FullScreen implements ResponseCallback 
{
    // MyScreen startScreen;
    private GIFEncodedImage _image;
    private int _currentFrame;
    private int _width, _height, _xPos, _yPos;
    private AnimatorThread _animatorThread;
    /**
     * 
     */
    public WaitScreen() {
        super(new VerticalFieldManager(), Field.NON_FOCUSABLE);
        setBackground(
                BackgroundFactory.createSolidTransparentBackground(Color.WHITE, 100));
        //this.startScreen = control.getPantalla();
        EncodedImage encImg = GIFEncodedImage.getEncodedImageResource("loader.gif");
        GIFEncodedImage img = (GIFEncodedImage) encImg;

        // Store the image and it's dimensions.
        _image = img;
        _width = img.getWidth();
        _height = img.getHeight();
        _xPos = (Display.getWidth() - _width) >> 1;
        _yPos = (Display.getHeight() - _height) >> 1;
        // Start the animation thread.
        _animatorThread = new AnimatorThread(this);
        _animatorThread.start();
        UiApplication.getUiApplication().pushScreen(this);
    }
    /**
     * 
     */
    protected void paint(Graphics graphics){
        for ( int i = 0; i <= _currentFrame; i ++ ) {
            graphics.drawImage(_xPos, _yPos,
                               _image.getFrameWidth(i),
                               _image.getFrameHeight(i),
                               _image,i,0, 0);
        }
    }
    /**
     * 
     */
    protected void onUndisplay() {
        _animatorThread.stop();
    }
    /**
     * 
     */
	public void callback(Producto data) {
		// TODO Auto-generated method stub
		
	}
    /**
     * 
     * @author ARMGARCES
     *
     */
    private class AnimatorThread extends Thread {
        private WaitScreen _theField;
        private boolean _keepGoing = true;
        private int _totalFrames, _loopCount, _totalLoops;
        /**
         * 
         * @param _theScreen
         */
        public AnimatorThread(WaitScreen _theScreen) {
                _theField = _theScreen;
                _totalFrames = _image.getFrameCount();
                _totalLoops = _image.getIterations();

        }
        /**
         * 
         */
        public synchronized void stop() {
                _keepGoing = false;
        }
        /**
         * 
         */
        public void run() {
            while (_keepGoing) {
                // Invalidate the field so that it is redrawn.
                UiApplication.getUiApplication().invokeAndWait(
                  new Runnable() {
                        public void run() {
                                _theField.invalidate();
                        }
                  });
                try {
	                  // Sleep for the current frame delay before
	                  // the next frame is drawn.
	                  sleep(_image.getFrameDelay(_currentFrame) * 10);
                } catch (InterruptedException iex) {
                
                } // Couldn't sleep.
                // Increment the frame.
                ++_currentFrame;
                if (_currentFrame == _totalFrames) {
	                  // Reset back to frame 0 
	                  // if we have reached the end.
	                  _currentFrame = 0;
	                  ++_loopCount;
	                  // Check if the animation should continue.
	                  if (_loopCount == _totalLoops) {
	                    _keepGoing = false;
	                  }
                }
            }
        }
    }
    /**
     * 
     */
	public void inicioSesion(Sesion _sesion) {
		// TODO Auto-generated method stub

	}
	public void restaurarSesion(Sesion _sesion) {
		// TODO Auto-generated method stub
		
	}
}